package com.lancoo.znbkxx.teacher.widget.newBarChart;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;

import androidx.annotation.RequiresApi;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.lancoo.znbkxx.teacher.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dadong on 2020/3/19
 * Describe:
 */
public class CheckScoreBarChart extends BarChart {

//    private ScoreClickListener scoreClickListener;

    public CheckScoreBarChart(Context context) {
        super(context);
    }

    public CheckScoreBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckScoreBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void showBarChart(final List<String> arrayTypeName, List<Integer> arrayTypeEntity, int xLableCount, int yLableCount, float yMaxValue, boolean moreColors) {
        setDrawBarShadow(false);
        setPinchZoom(false);
        setDrawGridBackground(false);
        getDescription().setEnabled(false);
        animateY(1000);
        setDragDecelerationEnabled(true);//拖拽滚动时，手放开是否会持续滚动，默认是true（false是拖到哪是哪，true拖拽之后还会有缓冲）
        zoom(getLableGroupSize(arrayTypeName.size(), xLableCount), 1f, 0, 0);//显示的时候 是 按照多大的比率缩放显示   1f表示不放大缩小

        setPinchZoom(false);
        setScaleEnabled(false);//禁止缩放
        setDragEnabled(true);// 是否可以拖拽
        setExtraBottomOffset(6);

        //设置说明
        Legend l = getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setForm(Legend.LegendForm.CIRCLE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);
        l.setEnabled(false);
        setData(arrayTypeEntity, moreColors);

        LimitLine ll1 = new LimitLine(0, "查看成绩");
        ll1.setLineWidth(4f);
        ll1.enableDashedLine(10f, 10f, 0f);//三个参数，第一个线宽长度，第二个线段之间宽度，第三个一般为0，是个补偿
        ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
        ll1.setTextSize(10f);
        ll1.setEnabled(false);//隐藏平均线

        //x轴数据
        XAxis xAxis = getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//位置
        xAxis.setCenterAxisLabels(true);//设置在轴中间
        xAxis.setDrawGridLines(false);//轴线
        xAxis.setLabelCount(xLableCount + 1, false);    //设置显示标签书，FALSE为自动调整
        xAxis.setGranularity(1f);//缩放时的最小距离
        xAxis.setTextSize(10);
        xAxis.setTextColor(getContext().getColor(R.color.c999999));
        //显示x轴标签
        if (arrayTypeName.size() != 0) {
            xAxis.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    if (value == 3f) {
                        return "不合格";
                    }
                    if (value == 2f) {
                        return "合格";
                    }
                    if (value == 1f) {
                        return "良好";
                    }
                    if (value == 0f) {
                        return "优秀";
                    }
                    return "";
                }
            });
        }

        //y轴数据
        YAxis leftAxis = getAxisLeft();
        leftAxis.addLimitLine(ll1);
        leftAxis.setDrawGridLines(true);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        leftAxis.setLabelCount(yLableCount, false);

        leftAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return String.format("%.0f", value);
            }
        });
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(0f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        leftAxis.setAxisMaximum(yMaxValue + yMaxValue / 100);  //动态设置最大,
        leftAxis.setTextColor(getContext().getColor(R.color.c999999));

        YAxis rightAxis = getAxisRight();
        rightAxis.setEnabled(false);
    }

    private void setData(List<Integer> yValueEntity, boolean moreColors) {

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < yValueEntity.size(); i++) {
            yVals1.add(new BarEntry(i + 0.5f, yValueEntity.get(i)));
        }

        BarDataSet set1 = new BarDataSet(yVals1, "查看成绩");
        set1.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                int n = (int) value;
                return n + "";
            }
        });
        set1.setDrawIcons(false);
        mRenderer = new CheckScoreBarChartRenderer(this, mAnimator, mViewPortHandler, moreColors);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setBarWidth(0.3f);//设置柱子的宽度
        setData(data);
    }

    private float getLableGroupSize(int float1, int float2) {
        float num = (float) float1 / float2;
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数，.后跟几个零代表几位小数
        return Float.valueOf(df.format(num));
    }

}
